<extend name="Base/base" />
<block name="button">
	<button type="submit" id="export" form="export-form" class="btn btn-primary" href="javascript:;"><i class="fa fa-reply"></i> {:L('button_backup')}</button>
        <a id="optimize" class="btn btn-primary" href="{:U('optimize')}"><i class="fa fa-hand-lizard-o"></i>  {:L('button_optimization')}</a>
        <a id="repair" class="btn btn-primary" href="{:U('repair')}"><i class="fa fa-legal"></i>  {:L('button_repair')}</a>
</block>
<block name="body">
	<div class="main" id="main">
		<div id="top-alert" class="fixed alert alert-danger" style="display: none;">
            <button class="close fixed" style="margin-top: 4px;">&times;</button>
            <div class="alert-content"></div>
        </div>
	</div>
	<div id="msg" class="text-info">
		
	</div>
	<div class="panel panel-default">
		<div class="panel-heading">
			<h3><i class="fa fa-list"></i>  {$heading_title}</h3>
		</div>
		<div class="panel-body">
			<form method="post" action="{:U('export')}" enctype="multipart/form-data" id="export-form">
				<table class="table table-bordered table-hover">
					<thead>
						<tr>
						<th style="width:50px;"><input type="checkbox" id="select-all"></th>
						<th>{:L('column_table_name')}</th>
						<th>{:L('column_data_quantity')}</th>
						<th>{:L('column_data_size')}</th>
						<th>{:L('column_create_date')}</th>
						<th>{:L('column_status')}</th>
						<th class="text-right">{:L('text_action')}</th>
						</tr>
					</thead>
					<tbody>
						<foreach name="list" item="item">
						<tr>
							<td><input type="checkbox" name="tables[]" value="{$item.name}"></td>
							<td>{$item.name}</td>
							<td>{$item.rows}</td>
							<td>{$item.data_length|format_bytes}</td>
							<td>{$item.create_time}</td>
							<td class="info">{:L('text_no_backup')}</td>
							<td class="text-right">
								
							</td>
						</tr>
						</foreach>
					</tbody>
				</table>
			</form>
		</div>
		<div class="panel-footer">
			<nav>
				<ul class="pagination">
				
				</ul>
			</nav>
		</div>
	</div>
</block>
<block name="script">
	<script>

		(function($){
        var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");
            $msg=$("#msg");
        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.status){
                    updateAlert(data.info,'alert-success');
                } else {
                    updateAlert(data.info,'alert-danger');
                }
                setTimeout(function(){
	                $('#top-alert').find('button').click();
	                $(that).removeClass('disabled').prop('disabled',false);
	            },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.children('i').removeClass().addClass('fa fa-spinner fa-pulse');
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.status){
                        tables = data.tables;
                        $msg.html(data.info + "{:L('warning_do_not_close_backup_start')}");
                        backup(data.tab);
                        window.onbeforeunload = function(){ return "{:L('warning_backuping')}" }
                    } else {
                        updateAlert(data.info,'alert-danger');
                        $export.parent().children().removeClass("disabled");
                        $export.children('i').removeClass().addClass('fa fa-reply');
                        setTimeout(function(){
        	                $('#top-alert').find('button').click();
        	                $(that).removeClass('disabled').prop('disabled',false);
        	            },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "{:L('text_backup_start')}");
            $.get($form.attr("action"), tab, function(data){
                if(data.status){
                    showmsg(tab.id, data.info);

                    if(!$.isPlainObject(data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.children('i').removeClass().addClass('fa fa-reply');
                        updateAlert('{:L("text_click_rebackup")}','alert-success');
                        $msg.html("");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    updateAlert(data.info,'alert-danger');
                    $export.parent().children().removeClass("disabled");
                    $export.children('i').removeClass().addClass('fa fa-reply');
                    setTimeout(function(){
    	                $('#top-alert').find('button').click();
    	                $(that).removeClass('disabled').prop('disabled',false);
    	            },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);

	</script>
</block>